package com.service.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.base.BaseService;
import com.entity.sys.SysRole;
import com.entity.sys.SysRoleDept;
import com.entity.sys.SysRoleMenu;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.sys.SysRoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 角色信息表 服务实现类
 */
@Service
public class SysRoleService extends BaseService<SysRoleMapper, SysRole>{

    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysRoleDeptService sysRoleDeptService;

    /**
     * 角色分页列表
     */
    public PageInfo<SysRole> page(Map<String,String> query) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(query.get("name"))) {
            queryWrapper.like("role_name", query.get("name"));
        }
        if(StrUtil.isNotBlank(query.get("code"))) {
            queryWrapper.like("role_key", query.get("code"));
        }
        queryWrapper.orderByDesc("create_time");
        PageHelper.startPage(Integer.parseInt(query.get("pageNum")),Integer.parseInt(query.get("pageSize")));
        return new PageInfo<>(baseMapper.selectList(queryWrapper));
    }

    /**
     * 获取角色id，根据角色名称
     */
    public String getIdByRoleName(String roleName){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", roleName);
        SysRole role = baseMapper.selectOne(queryWrapper);
        if (role != null){
            return role.getId();
        }
        return null;
    }

    /**
     * 根据多个id查询角色
     */
    public List<SysRole> getByIds(String id){
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.apply("FIND_IN_SET(id,'"+id+"')");
        query.select("id,role_key,role_name,data_scope");
        return baseMapper.selectList(query);
    }

    /**
     * 生成id（数字，从小到大生成）
     * 普通用户角色id默认是最大的
     */
    public String createId(){
        // 先将普通用户角色的id查出来
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.eq("role_key","PTYH");
        String ptyhId = baseMapper.selectOne(query).getId();
        QueryWrapper<SysRole> query1 = new QueryWrapper<>();
        query1.ne("id",ptyhId); // 排除普通用户
        query1.select("id");
        query1.orderByDesc("id"); // id倒序排序
        query1.last("limit 1"); // 取id最大的那一个
        String id = baseMapper.selectOne(query1).getId();
        String createId = String.valueOf(Integer.parseInt(id) + 1);
        if (ptyhId.equals(createId)){
            createId = String.valueOf(Integer.parseInt(ptyhId) + 1);
        }
        return createId;
    }

    /**
     * 校验角色名称和角色代码是否唯一
     */
    public boolean checkRoleNameAndKey(String roleName, String roleKey,String id) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(roleName)) {
            queryWrapper.eq("role_name", roleName);
        }
        if(StrUtil.isNotBlank(roleKey)) {
            queryWrapper.eq("role_key", roleKey);
        }
        if (StrUtil.isNotBlank(id)){
            queryWrapper.ne("id", id);
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        return count>0 ? true:false;
    }

    /**
     * 新增角色菜单信息
     */
    public void insertRoleMenu(String id, String[] menuIds) {
        // 删除角色与菜单关联
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenu::getRoleId,id);
        sysRoleMenuService.remove(queryWrapper);
        List<SysRoleMenu> list = new ArrayList<>();
        // 新增角色与菜单关联
        for (String menuId : menuIds) {
            list.add(new SysRoleMenu(id,menuId));
        }
        if (!list.isEmpty()){
            sysRoleMenuService.insertBatch(list);
        }
    }

    /**
     * 新增角色部门信息
     */
    public void insertRoleDept(String id, String[] deptIds) {
        // 删除角色与部门关联
        QueryWrapper<SysRoleDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleDept::getRoleId,id);
        sysRoleDeptService.remove(queryWrapper);
        List<SysRoleDept> list = new ArrayList<>();
        // 新增角色与部门关联
        for (String deptId : deptIds) {
            list.add(new SysRoleDept(id,deptId));
        }
        if (!list.isEmpty()){
            sysRoleDeptService.insertBatch(list);
        }
    }
}
